package com.kmust.dream.wanderingdream.upload.service.impl;

import com.kmust.dream.wanderingdream.upload.dto.ImageDTO;
import com.kmust.dream.wanderingdream.upload.service.UploadService;
import com.kmust.dream.wanderingdream.upload.utils.UploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class UploadServiceImpl implements UploadService {

    @Autowired
    private UploadUtils uploadUtils;

    @Override
    public String getUrl(MultipartFile multipartFile) {
        String url = uploadUtils.uploadDocument(multipartFile);
        log.info("url是{}", url);
        return url;
    }

    @Override
    public List<String> getUrls(MultipartFile[] files) {
        List<String> res = new ArrayList<>();
        for (MultipartFile file : files) {
            String s = uploadUtils.uploadDocument(file);
            res.add(s);
        }
        return res;
    }

    @Override
    public String resizeImage(ImageDTO imageDTO) {
        String p = "?x-oss-process=image/resize,h_" + imageDTO.getHeight() + ",w_" + imageDTO.getWidth();
        String resizeStr = imageDTO.getUrl() + p;
        log.info("拼接的url是{}", resizeStr);
        return resizeStr;
    }

}
